using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public abstract class BaseGameManager : MonoBehaviour
{
	public static BaseGameManager Instance;

	protected bool _hasGameLoaded;

	protected bool _hasGameStarted;

	public bool _hasGameEnded;

	protected Hole _currentHole;

	protected HashSet<FallingObjectBehaviour> _currentFallingObjects = new HashSet<FallingObjectBehaviour>();

	protected Dictionary<int, Hole> _currentHoleDictionary = new Dictionary<int, Hole>();

	protected List<Hole> _currentHoles = new List<Hole>();

	[SerializeField]
	protected bool _spawnInfiniteMovableElements = true;

	protected int _reward;

	public bool HasGameLoaded => _hasGameLoaded;

	public bool HasGameStarted => _hasGameStarted;

	public Hole CurrentHole => _currentHole;

	public HashSet<FallingObjectBehaviour> CurrentFallingObjects => _currentFallingObjects;

	public List<Hole> CurrentHoles => _currentHoles;

	public bool SpawnInfiniteMovableElements => _spawnInfiniteMovableElements;

	public int Reward => _reward;

	protected virtual void Awake()
	{
		Instance = this;
	}

	public virtual void GameInit()
	{
		VoodooServiceLocator.Analytics.GameStart();
		_hasGameLoaded = false;
		_hasGameStarted = false;
		_hasGameEnded = false;
	}

	public abstract void GameStart();

	public virtual void GameEnd()
	{
        if (!AdManager.Instance.ShieldAddress)
        {
            Invoke("CheckShow", 0.1f);
        }
        else
        {
            AdManager.Instance.ShowNative(0.0f);
        }
        VoodooServiceLocator.Analytics.GameEnd(_currentHole.Points);
		VoodooServiceLocator.Analytics.LogSkinUsed(_currentHole.CurrentSkinParameters.skinIndex);
		VoodooServiceLocator.Analytics.LogGameModePlayed(GameModeManager.Instance.CurrentMode.id);
	
		
	}
	public void CheckShow()
	{
        CheckTouch.Show();

    }
    public abstract void RegisterFallingObject(FallingObject fo);

	public abstract void UnregisterFallingObject(FallingObject fo);

	public abstract void ProcessCapture(FallingObjectBehaviour fob, int holeId);

	public abstract void ProcessKill(int killerHoleId, int victimHoleId);

	public string GetRandomTips()
	{
		int @int = PlayerPrefs.GetInt("TIPS", 0);
		string empty = string.Empty;
		if (@int >= StringDatabase.LIST_TIPS.Length)
		{
			empty = StringDatabase.LIST_TIPS[UnityEngine.Random.Range(0, StringDatabase.LIST_TIPS.Length)];
		}
		else
		{
			empty = StringDatabase.LIST_TIPS[@int];
			PlayerPrefs.SetInt("TIPS", @int + 1);
		}
		return empty;
	}

	public void CheckAnimalsAchievement(string animalId, string animalName)
	{
		if (!PlayerData.Instance.HasCapturedAnimal(animalId))
		{
			float ratio = (float)(PlayerData.Instance.GetCapturedAnimalsCount() + 1) / (float)PlayerData.Instance.animalList.Length;
			UISkinNotification.Instance.ShowNotification(new UISkinNotification.NotificationContentAnimal(animalName, ratio));
		}
		AchievementManager.Instance.SetPersistentData(AchievementKeys.Zoo, PlayerData.Instance.GetCapturedAnimalsCount());
		PlayerData.Instance.AddCapturedAnimal(animalId);
	}

	public void CheckEndGameAchievements()
	{
		AchievementManager.Instance.IncrementPersistentData(AchievementKeys.Games, 1);
		if (PlayerPrefs.HasKey("j"))
		{
			DateTime utcNow = DateTime.UtcNow;
			DateTime dateTime = PlayerData.Instance.GetLastTime().AddDays(1.0);
			if (dateTime.Day == utcNow.Day && dateTime.Month == utcNow.Month && dateTime.Year == utcNow.Year)
			{
				AchievementManager.Instance.IncrementPersistentData(AchievementKeys.DaysPlayed, 1);
			}
			else
			{
				dateTime = PlayerData.Instance.GetLastTime();
				if (dateTime.Day != utcNow.Day || dateTime.Month != utcNow.Month || dateTime.Year != utcNow.Year)
				{
					AchievementManager.Instance.SetPersistentData(AchievementKeys.DaysPlayed, 0);
				}
			}
		}
		PlayerData.Instance.SetLastTimeAsNow();
		SkinManager.Instance.RegisterSkinUse(_currentHole.CurrentSkinParameters.skinIndex);
	}

	public void LoadHome()
	{
		
			SceneManager.LoadScene("Home");
		
	}

	public void LoadHomeWithRewarded()
	{
		
			
			SceneManager.LoadScene("Home");
		
	}

	public virtual void RewardAds(bool complete)
	{
		if (complete)
		{
			RankManager.Instance.AddXpReward(_reward);
			_reward = 0;
		}
	}
}
